package com.dwd.permis.controller;

import com.dwd.basic.query.BaseQuery;
import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.check.query.ShopQuery;
import com.dwd.org.pojo.Shop;
import com.dwd.permis.pojo.EmpRole;
import com.dwd.permis.pojo.Role;
import com.dwd.permis.service.IPermisService;
import com.dwd.permis.service.IRoleService;
import com.dwd.pet.service.IPetService;
import com.dwd.servicemanager.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/2/20 16:13
 * @Version 1.0
 */
@RestController
@RequestMapping("/role")
public class PermisController {
    @Autowired
    private IPermisService permisService;
    @Autowired
    private IRoleService roleService;

    /**
     * 查询角色
     *
     * @param query
     * @return
     */
    @PostMapping("/queryRoles")
    public PageBean<Role> queryRoles(@RequestBody BaseQuery query) {
        return permisService.queryPage(query);
    }

    /**
     * 查询角色，带上用户
     *
     * @param query
     * @return
     */
    @PostMapping("/queryEmpRoles")
    public PageBean<EmpRole> queryEmpRoles(@RequestBody BaseQuery query) {
        return roleService.queryPage(query);
    }

    @PostMapping("/queryPermis/{id}")
    public List<Long> queryPermis(@PathVariable("id") Long id) {
        return permisService.queryPermis(id);
    }


    /**
     * 授权
     *
     * @param role
     * @return
     */
    @PostMapping("/za")
    public AjaxResult za(@RequestBody Role role) {
        try {
            System.out.println(role);
            permisService.delete(role.getId());
            permisService.add(role.getId(), role.getIds());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 角色
     *
     * @param empRole
     * @return
     */
    @PostMapping("/addEmpRoles")
    public AjaxResult addEmpRoles(@RequestBody EmpRole empRole) {
        try {
            roleService.add(empRole.getId(), Long.valueOf(empRole.getRoleId()));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }
}
